<?php
class Sms
{
    public $ci;
    public $error_code = -1;
    public $error_msg = '';
    public $gateway = 'http://www.82009668.com:888/SDK/Service.asmx';
    public static $code_stack = array(
        -1 => '连接失败',
        -2 => '用户名密码错误',
        -3 => '账户被停用',
        -4 => '账户余额不足',
        -5 => '手机号码为空或者超过上限（1000个）',
        -6 => '特服号错误',
        -7 => '内容为空或者长度超过上限,传真文件格式错误,电话会议命令格式错误',
        -8 => '定时时间格式错误',
        -9 => '新密码为空',
        -10=> '彩信或者传真文件格式错误',
        -11=> '彩信文件超过大小（小于50K）,传真文件超过大小（小于4M）',
        -12=> '彩信缺少主题',
        -13=> '彩信内容为空白',
        -14=> '彩信帧数超过15帧',
        -15=> '您还未开通该类产品',
        -16=> '状态上报地址格式错误',
        -17=> '电话会议命令执行失败',
        1  => '操作成功',
    );

    public function __construct()
    {
        $this->ci =& get_instance();
        $this->ci->load->library('remote');
    }

    private function _getConfig()
    {

    }

    /**
     * [_handleXml description]
     * @param  string $type string, int
     * @return [type]       [description]
     */
    private function _handleXml($resp, $type = 'int')
    {
        $result = false;
        $doc = new DOMDocument;
        $doc->loadXML($resp);
        $element = $doc->getElementsByTagName($type);
        if ($element->length > 0) {
            $item = $element->item(0);
            $result = $item->nodeValue;
        }
        return $result;
    }

    /**
     * [getBalance description]
     * @param  [type] $username [description]
     * @param  [type] $password [description]
     * @return [type]           [description]
     */
    public function getBalance($username, $password)
    {
        $url = $this->gateway . '/getBalance';
        $params = array();
        $params['username'] = $username;
        $params['password'] = $password;
        log_message('debug', 'req_url :' . $url);
        $resp = $this->ci->remote->curl_request($url, $params);
        log_message('debug', 'req_data :' . json_encode($params));
        log_message('debug', 'rsp_data :' . $resp);
        return $this->_handleXml($resp);
    }

    public function sendMessage($username, $pwd, $phones, $contents, $scode='', $setTime='')
    {
        $url = $this->gateway . '/sendMessage';
        $params = array();
        $params['username'] = $username;
        $params['pwd'] = $pwd;
        $params['phones'] = $phones;
        $params['contents'] = $contents;
        $params['scode'] = $scode;
        $params['setTime'] = $setTime;
        log_message('debug', 'req_url :' . $url);
        $resp = $this->ci->remote->curl_request($url, $params);
        log_message('debug', 'req_data :' . json_encode($params));
        log_message('debug', 'rsp_data :'. $resp);
        return $this->_handleXml($resp);
    }

    public function changePassWord()
    {}

    public function sendMMS()
    {}

    public function sendChat()
    {}

    public function getChat()
    {}

    public function Meeting()
    {}

    public function sendVoice()
    {}

    public function sendFax()
    {}
}
?>

